//
//  ScrollTabBarController.swift
//  ScrollTabBarController
//
//  Created by 好米有易 on 2017/11/13.
//  Copyright © 2017年 好米有易. All rights reserved.
//

import UIKit


/// cell重用标识符
let CONTENT_CELLID = "CONTENT_CELLID"

class ScrollTabBarController: UIViewController {
    
   open lazy var tabBarView: TabBarItemIVew = {
        
        let tabView = TabBarItemIVew(frame: CGRect(x: 0, y: SCREEN_HEIGHT - TABBAR_HEIGHT, width: SCREEN_WIDHT, height: TABBAR_HEIGHT))
            tabView.delegate = self
        return tabView
    }()
    
    
   private lazy var contentView: UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        
            layout.itemSize = CGSize(width: SCREEN_WIDHT, height: SCREEN_HEIGHT - TABBAR_HEIGHT)
        
            layout.minimumLineSpacing = 0
        
            layout.minimumInteritemSpacing = 0
        
            layout.scrollDirection = UICollectionViewScrollDirection.horizontal
        
        let collectionV = UICollectionView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDHT, height: SCREEN_HEIGHT - TABBAR_HEIGHT), collectionViewLayout: layout)
    
            collectionV.register(UICollectionViewCell.self, forCellWithReuseIdentifier: CONTENT_CELLID)
            collectionV.showsHorizontalScrollIndicator = false
            collectionV.isPagingEnabled = true
            collectionV.bounces = false
            collectionV.delegate = self
            collectionV.dataSource = self
        return collectionV
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        setupUI()
    }
  
    
}


// MARK: - UICollectionViewDelegateFlowLayout, UICollectionViewDataSource
extension ScrollTabBarController: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
       return self.childViewControllers.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CONTENT_CELLID, for: indexPath)
        
        cell.backgroundColor = indexPath.row % 2 == 0 ? UIColor.red : UIColor.green
        
        return cell
        
        
    }
    
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let offsetX = scrollView.contentOffset.x + scrollView.bounds.width * 0.5
        
        let currentIndex = Int(offsetX / scrollView.bounds.width) % (self.childViewControllers.count)
        
        tabBarView.resetCurrentItem(index: currentIndex)
    }
    
}

// MARK: - TabBarItemViewDelegate
extension ScrollTabBarController: TabBarItemViewDelegate {
    
    func tapWith(index: NSInteger) {
        
        let indexPath: IndexPath = IndexPath(item: index, section: 0)
        
         contentView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.centeredHorizontally, animated: false)
    }
    
    
}




// MARK: - UI
extension ScrollTabBarController {
    
   private func setupUI() {
        
        view.addSubview(tabBarView)
    
        view.addSubview(contentView)
    
    
    }
    
    
    override func addChildViewController(_ childController: UIViewController) {
        super.addChildViewController(childController)
        
        _ = tabBarView.subviews.map {
            
            $0.removeFromSuperview()
            
        }
        
        tabBarView.load(items: childViewControllers)
        
        print(childViewControllers)
                
    }
}



